﻿using ComponentSystem;
using Cysharp.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace LubanSystem
{
    /// <summary>
    /// Luban服务的YooAssets插件
    /// </summary>
    public partial class LubanService_Entity_YooAssets : EntityCSharp
    {
        /// <summary>
        /// YooAssets包体读取
        /// </summary>
        protected YooAssets_Com yooAssets_Com => LubanService_Entity.Instance.GetComponentOrAdd<YooAssets_Com>();
    }

    public partial class LubanService_Entity_YooAssets
    {
        /// <summary>
        /// 将YooAssets的配置文件读取为Json字符串 -- 同步
        /// </summary>
        /// <param name="assetPath">配置文件路径</param>
        /// <param name="pkgName">包体名称</param>
        /// <returns>Json字符串</returns>
        public string LoadConfig_ToJson_YooAssets_Sync(string assetPath, string pkgName = "")
        {
            if (!this.yooAssets_Com.Exist(assetPath, pkgName)) return string.Empty;

            string config = this.yooAssets_Com.LoadConfig_Sync(assetPath, pkgName);

            return config;
        }

        /// <summary>
        /// 将YooAssets的配置文件读取为Json字符串 -- 异步
        /// </summary>
        /// <param name="assetPath">配置文件路径</param>
        /// <param name="pkgName">包体名称</param>
        /// <returns>Json字符串</returns>
        public async UniTask<string> LoadConfig_ToJson_YooAssets_Async(string assetPath, string pkgName = "")
        {
            if (!this.yooAssets_Com.Exist(assetPath, pkgName)) return string.Empty;

            string config = await this.yooAssets_Com.LoadConfig_Async(assetPath, pkgName);

            return config;
        }

        /// <summary>
        /// 将YooAssets的配置文件读取为JArray
        /// </summary>
        /// <param name="assetPath">配置文件路径</param>
        /// <param name="pkgName">包体名称</param>
        /// <returns>JArray</returns>
        public JArray LoadConfig_ToJArray_YooAssets(string assetPath, string pkgName = "")
        {
            string data = string.Empty;
            data = LoadConfig_ToJson_YooAssets_Sync(assetPath, pkgName);

            if (string.IsNullOrEmpty(data))
            {
                return JsonConvert.DeserializeObject<JArray>(string.Empty);
            }

            return JsonConvert.DeserializeObject<JArray>(data);
        }

        /// <summary>
        /// 将YooAssets的配置文件读取为JArray
        /// </summary>
        /// <param name="assetPath">配置文件路径</param>
        /// <param name="pkgName">包体名称</param>
        /// <returns>JArray</returns>
        public async UniTask<JArray> LoadConfig_ToJArray_YooAssets_UniTask(string assetPath, string pkgName = "")
        {
            string data = string.Empty;
            data = await LoadConfig_ToJson_YooAssets_Async(assetPath, pkgName);

            if (string.IsNullOrEmpty(data))
            {
                return JsonConvert.DeserializeObject<JArray>(string.Empty);
            }

            return JsonConvert.DeserializeObject<JArray>(data);
        }
    }
}